// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"

class ABattery;

class MISCELLANEOUSPROJECT_API OctreeNode : public TSharedFromThis<OctreeNode>
{
public:
	OctreeNode(FVector _center, FVector _extend, int32 _depth, TSharedPtr<OctreeNode> _root = nullptr) : center(_center), extend(_extend), depth(_depth)
	{
		root = _root;
		bInRange = false;
	}

	~OctreeNode()
	{
		root = nullptr;
		objs.Empty();
		children.Empty();
	}

	static bool IsNotUsed();
	// 立方体与球 求交
	bool InterSection(FVector OCenter, float Radian) const;
	// 点是否在本区域内
	bool InterSection(FVector _point) const;
	// 选择位置点在哪个象限
	int32 SelectBestChild(FVector _point) const;
	// 分割出八个子节点
	void Split();
	// 插入对象
	void InsertObject(ABattery* obj);
	// 合并可以起到优化的效果
	void Merge();
	bool CanMerge();
	//移除对象
	bool RemoveObject(ABattery* obj);
	// 绘制区域边界
	void DrawBound(float time = 0.02f, float thickness = 2.0f) const;
	// 判断电池是否在扫描器的范围类
	void TraceObjectInRange(AActor* traceActor, float _radian);
	void TraceObjectOutRange();
	// 更新状态，每帧调用
	void UpdateState();

public:
	// 中心点
	FVector center;
	// 扩展尺寸
	FVector extend;
	// 最小分割尺寸
	float miniSize = 20; 
	int32 maxCount = 4;
	int32 depth;

	TArray<ABattery*> objs;
	static UObject* worldObject;
	bool bInRange;

	TSharedPtr<OctreeNode> root;
	TArray<TSharedPtr<OctreeNode>> children;
};
